# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
from urllib.parse import urlencode


class Arrangement(object):

    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "一路平安交通工具意外伤害保险"

    # 生产数据
    def genProductInfo(self):

        plan = {
            "planCode": ["planA", "planB"],
            "days": ["7d", "30d", "90d", "180d", "365d"],
            "people_count": ["1"]
        }

        for v in product(plan["planCode"], plan["days"], plan["people_count"]):

            yield v

    # 执行爬虫
    def requestJson(self, data=None):

        for v in self.genProductInfo():

            if v[0] == "planA":

                print("/*基础款")

                result =  {

                    "airplane_death": "500000",
                    "train_death": "50000",
                    "bus_death":"50000",
                    "taxi_death":"50000",
                    "passenger_death":"50000",
                    "subway_death":"50000",
                    "ship_disability": "50000",
                    "travel_death":"50000",
                    "private_death":"50000",
                    "service_death":"50000"

                }

                data = {
                    "requestData": '{"productId":"060A_0804","isMulti":"1","issueChannel":"1","list":[{"cInsrncCde":"069400","nSelectFlag":"1","nPrm":"2.07","nAmt":"50万"},{"cInsrncCde":"069401","nSelectFlag":"1","nPrm":"0.1","nAmt":"5万"},{"cInsrncCde":"069402","nSelectFlag":"1","nPrm":"0.27","nAmt":"5万"},{"cInsrncCde":"069403","nSelectFlag":"1","nPrm":"0.16","nAmt":"5万"},{"cInsrncCde":"069404","nSelectFlag":"1","nPrm":"0.33","nAmt":"5万"},{"cInsrncCde":"069405","nSelectFlag":"1","nPrm":"0.16","nAmt":"5万"},{"cInsrncCde":"069406","nSelectFlag":"1","nPrm":"0.1","nAmt":"5万"},{"cInsrncCde":"069407","nSelectFlag":"1","nPrm":"0.16","nAmt":"5万"},{"cInsrncCde":"069408","nSelectFlag":"1","nPrm":"0.9","nAmt":"5万"},{"cInsrncCde":"069409","nSelectFlag":"1","nPrm":"1.2","nAmt":"5万"}],"productPlanId":"4e62d9ed-10e7-4c14-92ba-a2ea46588915","securityPeriod":"D#' +
                                   v[1].replace("d", "") + '"}'}

                payload = urlencode(data).replace("+", "")

                r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)

                if r.status_code == 200:

                    price = (Helper.decimalParse(r.json()["tPremium"]))

                    data = {
                        "name":self.name,
                        "company":"华安保险",
                        "days":v[1],
                        "people_count":"1",
                        "price":price
                    }

                    data.update( result )

                    self.mergeTable( data )



            else:
                print("/*尊享款")

                result =  {

                    "airplane_death": "1000000",
                    "train_death": "200000",
                    "bus_death": "200000",
                    "taxi_death": "200000",
                    "passenger_death": "200000",
                    "subway_death": "200000",
                    "ship_disability": "200000",
                    "travel_death": "200000",
                    "private_death": "100000",
                    "service_death": "100000"

                }
                data = {
                    "requestData": '{"productId":"060A_0804","isMulti":"1","issueChannel":"1","list":[{"cInsrncCde":"069400","nSelectFlag":"1","nPrm":"0.55","nAmt":"100万"},{"cInsrncCde":"069401","nSelectFlag":"1","nPrm":"0.12","nAmt":"20万"},{"cInsrncCde":"069402","nSelectFlag":"1","nPrm":"0.22","nAmt":"20万"},{"cInsrncCde":"069403","nSelectFlag":"1","nPrm":"0.13","nAmt":"20万"},{"cInsrncCde":"069404","nSelectFlag":"1","nPrm":"0.26","nAmt":"20万"},{"cInsrncCde":"069405","nSelectFlag":"1","nPrm":"0.13","nAmt":"20万"},{"cInsrncCde":"069406","nSelectFlag":"1","nPrm":"0.13","nAmt":"20万"},{"cInsrncCde":"069407","nSelectFlag":"1","nPrm":"0.12","nAmt":"20万"},{"cInsrncCde":"069408","nSelectFlag":"1","nPrm":"0.36","nAmt":"10万"},{"cInsrncCde":"069409","nSelectFlag":"1","nPrm":"0.48","nAmt":"10万"}],"productPlanId":"d4976311-030f-48d2-9584-b5e16670a5d4","securityPeriod":"D#'+v[1].replace("d","")+'"}'}


                payload = urlencode(data).replace("+", "")

                r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)

                if r.status_code == 200:

                    price = (Helper.decimalParse(r.json()["tPremium"]))

                    data = {
                        "name": self.name,
                        "company": "华安保险",
                        "days": v[1],
                        "people_count": "1",
                        "price": price
                    }
                    data.update( result )

                    self.mergeTable( data )

    # 合并表格
    def mergeTable(self, data):

        SQL = 'INSERT INTO `'+TABLE_ACCIDENT+'`(`name`,`company`,`days`,`people_count`,`price`,`airplane_death`,`train_death`,`bus_death`,`taxi_death`,`passenger_death`,`subway_death`,`ship_disability`,`travel_death`,`private_death`,`service_death`)VALUES("{name}","{company}","{days}","{people_count}","{price}","{airplane_death}","{train_death}","{bus_death}","{taxi_death}","{passenger_death}","{subway_death}","{ship_disability}","{travel_death}","{private_death}","{service_death}")'

        print(SQL.format(**data))

        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()


if __name__ == '__main__':
    a = Arrangement()
    a.requestJson()
